
package com.platform.modules.event.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Query;
import com.platform.modules.event.dao.EventCalendarBranchDao;
import com.platform.modules.event.entity.EventCalendarBranchEntity;
import com.platform.modules.event.service.EventCalendarBranchService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 活动日历支行关系表Service实现类
 *
 * @author
 * @date 2023-04-27 10:50:42
 */
@Service("eventCalendarBranchService")
public class EventCalendarBranchServiceImpl extends ServiceImpl<EventCalendarBranchDao, EventCalendarBranchEntity> implements EventCalendarBranchService {

    @Override
    public List<EventCalendarBranchEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.branchId");
        params.put("asc", false);
        Page<EventCalendarBranchEntity> page = new Query<EventCalendarBranchEntity>(params).getPage();
        return page.setRecords(baseMapper.selectEventCalendarBranchPage(page, params));
    }

    @Override
    public boolean add(EventCalendarBranchEntity eventCalendarBranch) {
        return this.save(eventCalendarBranch);
    }

    @Override
    public boolean update(EventCalendarBranchEntity eventCalendarBranch) {
        return this.updateById(eventCalendarBranch);
    }

    @Override
    public boolean delete(String branchId) {
        return this.removeById(branchId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] branchIds) {
        return this.removeByIds(Arrays.asList(branchIds));
    }

    @Override
    public void saveOrUpdate(String id, List<String> branchIdList) {
        Map<String, Object> map = new HashMap<>();
        map.put("calendar_id", id);
        //先删除活动与支行关系
        this.removeByMap(map);

        if (branchIdList == null || branchIdList.size() == 0) {
            return;
        }

        //保存活动与卡种关系
        List<EventCalendarBranchEntity> list = new ArrayList<>(branchIdList.size());
        for (String branchId : branchIdList) {
            EventCalendarBranchEntity entity = new EventCalendarBranchEntity();
            entity.setCalendarId(id);
            entity.setBranchId(branchId);
            list.add(entity);
        }
        this.saveBatch(list);
    }

    @Override
    public List<String> queryBranchIdList(String id) {
        return baseMapper.queryBranchIdList(id);
    }
}
